﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Common.Startup
{
    /// <summary>
    /// 系统启动参数集合
    /// </summary>
    public class StartupParamCollection
    {
        private List<StartupParam> _paramList;
        public StartupParamCollection()
        {
            _paramList = new List<StartupParam>();
        }

        public StartupParam this[int index]
        {
            get
            {
                return _paramList[index];
            }
        }

        public int Count
        {
            get
            {
                return _paramList.Count;
            }
        }

        public void Add(StartupParam param)
        {
            _paramList.Add(param);
        }

        /// <summary>
        /// 通过参数索引，获取参数信息
        /// </summary>
        /// <param name="index">参数索引</param>
        /// <returns></returns>
        public StartupParam GetAt(int index)
        {
            return _paramList[index];
        }

        /// <summary>
        /// 通过参数名称，获取参数信息
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <returns></returns>
        public StartupParam Get(string name)
        {
            string upperName = name.ToUpper();
            for(int index = 0; index < _paramList.Count; index++)
            {
                if(_paramList[index].Name == upperName)
                {
                    return _paramList[index];
                }
            }
            return null;
        }

        public string GetValueAt(int index)
        {
            StartupParam param = _paramList[index];
            if (param == null)
            {
                return string.Empty;
            }
            return param.Value;
        }

        public string GetValue(string name)
        {
            StartupParam param = Get(name);
            if (param == null)
            {
                return string.Empty;
            }
            return param.Value;
        }

        /// <summary>
        /// 是否检查版本,true：需要检查版本,false：不需要检查版本
        /// </summary>
        public bool IsCheckVersion
        {
            get
            {
                bool isCheckVersion = false;
                string value = GetValue(StartupParam.PARAM_CHECKVERSION);
                if (bool.TryParse(value, out isCheckVersion))
                {
                    return isCheckVersion;
                }
                return true;
            }
        }
    }
}
